package com.asiainfo.indb.view;

import com.asiainfo.indb.PanelUtil;
import com.asiainfo.indb.control.db.OracleBatchFileLineInDb;
import com.asiainfo.indb.listener.PlaceholderListener;
import com.asiainfo.indb.model.FileGather;
import com.asiainfo.indb.model.InDbGather;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;

/**
 * 控制区
 *
 * @author zym
 * @date 2019-10-17 12:46
 */
public class ControlPanel extends BaseTitlePanel {

    private JTextField splitField;
    private JTextField fileEncodeField;
    private JTextField skipField;
    private JTextField batchField;
    private JTextField parallelField;

    public ControlPanel(InDbPanel parentPanel) {
        super(parentPanel);
    }

    @Override
    protected String getPanelTitle() {
        return "入库控制";
    }

    @Override
    protected void render(JPanel container) {
        container.setLayout(new GridLayout(3, 1, 2, 5));

        // 分隔符，编码
        renderSplitCd(container);
        // 跳过首行数，批量提交数
        renderBatch(container);
        // 并行数量（默认为当前cpu*2），如果指定0则单线程
        renderParallel(container);
    }

    private void renderParallel(JPanel container) {
        parallelField = new JTextField();

        // 线程数默认显示为cpu*2
        parallelField.addFocusListener(new PlaceholderListener(parallelField, "并行数默认为当前cpu*2，如果指定0则单线程"));

        // 加到父级
        JPanel panel = PanelUtil.generateOneGridInputPanel("并行数：", parallelField, 6);
        container.add(panel);
    }

    private void renderBatch(JPanel container) {
        skipField = new JTextField();
        skipField.setText("0");

        batchField = new JTextField();
        batchField.setText(OracleBatchFileLineInDb.DEF_PER_BATCH + "");

        JPanel panel = PanelUtil.generateTwoGridInputPanel("跳过首行数：", skipField, "批量提交数：", batchField, 6);
        container.add(panel);
    }

    private void renderSplitCd(JPanel container) {
        splitField = new JTextField();
        splitField.setText("|");

        fileEncodeField = new JTextField();
        fileEncodeField.setText("UTF-8");
        JPanel panel = PanelUtil.generateTwoGridInputPanel("分隔符：", splitField, "编码格式：", fileEncodeField, 6);
        container.add(panel);
    }

    @Override
    public InDbGather gather() {
        InDbGather gather = new InDbGather();
        if (checkAndSetField("分隔符不能为空", splitField, "SPLIT", gather)
                && checkAndSetField("编码格式不能为空", fileEncodeField, "FILE_ENCODE", gather)
                && checkAndSetField("请设置跳过首行数", skipField, "SKIP_NUMBER", gather)
                && checkAndSetField("请设置批量提交数", batchField, "BATCH_NUMBER", gather)) {
            // 并行数
            String parallel = parallelField.getText();
            if (!StringUtils.isNumeric(parallel)) {
                // 非数字默认设置为cpu*2
                parallel = Runtime.getRuntime().availableProcessors() * 2 + "";
            }

            gather.put("PARALLEL", parallel);

            return gather;
        }

        return null;
    }

    @Override
    public void initByFile(FileGather gather) {
        initField("SPLIT", splitField, gather);
        initField("FILE_ENCODE", fileEncodeField, gather);
        initField("SKIP_NUMBER", skipField, gather);
        initField("BATCH_NUMBER", batchField, gather);
        initField("PARALLEL", parallelField, gather);
    }
}